# encoding:utf-8
from flask import Flask, request, jsonify, redirect, url_for, render_template
import os
from unipath import FILES, Path, DIRS

# 用当前脚本名称实例化Flask对象，方便flask从该脚本文件中获取需要的内容
app = Flask(__name__)

local_youma_mapping_dir_path = r'E:\emby\文件映射\有码艺术'
local_wuma_mapping_dir_path = r'E:\emby\文件映射\无码艺术'


# 启动postplayer
@app.route('/launched_potplayer', methods=["GET"])
def launched_potplayer():
    # 假设有如下 URL
    # http://127.0.0.1:5000/index?name=john&age=20

    # 获取文件路径
    file_path = request.args.get("file_path")
    if not file_path:
        return "文件路径为空"

    new_file_path = ''
    # E:\emby\有码艺术\ABF-055\ABF-055.mp4
    # Y:\115\艺术视频\有码艺术\ABF-055.mp4
    if file_path.__contains__('艺术视频'):
        new_file_path = Path(file_path.replace(r'E:\emby', r'Y:\115'))
        new_file_path = Path(new_file_path.parent.parent, new_file_path.name)
    else:
        new_file_path = Path(file_path.replace(r'E:\emby', r'Y:\115\影视剧集'))
    print(file_path)
    print(new_file_path)
    # 将cmd的显示字符编码从默认的GBK改为UTF-8
    os.system('chcp 65001')
    # os.system()默认阻塞当前程序执行，在 Windows 命令前加入 start可不阻塞当前程序执行。如下：
    # 执行命令
    cmd = '"D:\Program Files\DAUM\PotPlayer\PotPlayerMini64.exe" "{}"'.format(new_file_path)
    print(cmd)
    os.popen(cmd)

    return "命令执行成功"


if __name__ == '__main__':
    app.run()
